<template>
  <a-popover v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" icon="user" :src="currentUser.avatar" class="antd-pro-global-header-index-avatar" />
      <span class="user-name">{{ currentUser.name }}</span>
      <span><a-icon type="down"></a-icon></span>
    </span>
    <template v-slot:content>
      <div class="menu">
        <div class="user-info">
          <div class="avatar">
            <a-avatar :size="48" icon="user" :src="currentUser.avatar" />
          </div>
          <div class="content">
            <div class="user-name">{{ currentUser.name }}</div>
            <div class="des">{{ currentUser.job }}</div>
          </div>
          <div class="reset"><a @click="showPassword">修改密码</a></div>
        </div>
        <a-list item-layout="horizontal" :data-source="data">
          <a-list-item slot="renderItem" slot-scope="item">
            <a-list-item-meta>
              <div slot="title">{{ item.title }}</div>
            </a-list-item-meta>
            <div>{{ item.value }}</div>
          </a-list-item>
        </a-list>
        <div style="padding-top: 16px">
          <a-button block @click="handleLogout">退出</a-button>
        </div>
      </div>
    </template>
  </a-popover>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import EditPassword from './edit-password.vue'

export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    data () {
      return [
        {
          title: '部门',
          value: this.currentUser.department || ''
        },
        {
          title: '电话',
          value: this.currentUser.phone || ''
        },
        {
          title: '邮箱',
          value: this.currentUser.email || ''
        }
      ]
    }
  },
  methods: {
    showPassword () {
      this.$dialog(
        EditPassword,
        {},
        {
          title: '修改密码'
        }
      )
    },
    handleLogout (e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel () {}
      })
    },

    handleCancel () {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../style/_var.less';
.menu {
  padding: 12px 8px;
  width: 288px;
  background: #ffffff;
  .user-info {
    display: flex;
    padding-bottom: 16px;
    color: @text-color;
    .avatar {
      margin-right: 16px;
    }
    .content {
      flex: 1;
    }
    .user-name {
      padding-bottom: 2px;
      font-size: 18px;
      font-weight: bold;
    }
    .des {
      font-size: @font-size-base;
    }
  }
  .reset {
    font-size: @font-size-base;
  }
}
</style>
